package dcc.component;

import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.JOptionPane;

import dcc.tools.Tools;

public class DateTimeField extends CustomTextField {

	public DateTimeField() {
		addFocusListener(new FocusAdapter() {
			@Override
			public void focusLost(FocusEvent e) {
				checkText();
			}
		});
	}

	public boolean checkText() {
		if (!this.getText().equals("")) {
			Pattern p = Pattern
					.compile("\\d{4}-[01]\\d-[0123]\\d|\\d{4}-[01]\\d|\\d{4}");
			Matcher m = p.matcher(this.getText());
			if (!m.matches()) {
				JOptionPane.showMessageDialog(null,
						Tools.get().getValue("LABEL_DATETIME_FORMAT"), Tools
								.get().getValue("LABEL_PROMPT_TITLE"),
						JOptionPane.INFORMATION_MESSAGE);
				this.setText("");
				this.requestFocus();
				return false;
			} else {
				return true;
			}
		}
		return true;
	}
}
